<script lang="ts" setup>
import {useRoute, useRouter} from "vue-router";
import type {PropType} from "vue";

const route = useRoute();
const router = useRouter();

interface Book {
  bookId: number;
  bookName: string;
  authorName: string;
  bookDesc: string;
  picUrl: string;
}

const props = defineProps({
  recommends: {
    type: Array as PropType<Book[]>,
    default: () => []
  },
  imageBaseUrl: {
    type: String,
    default: "",
  }
});
const {recommends, imageBaseUrl} = props;
const bookDetail = (bookId: any) => {
  router.push({path: `/book/${bookId}`});
};
</script>

<template>
  <div class="left-box">
    <div class="title">
      <slot name="title"></slot>
    </div>
    <div class="book-content">
      <div v-for="(book,index) in recommends" class="book-item">
        <img :src="imageBaseUrl+book.picUrl" alt="" class="book-img" @click="bookDetail(book.bookId)">
        <div class="book-info">
          <h4 class="book-name" @click="bookDetail(book.bookId)">{{ book.bookName }}</h4>
          <p class="book-author">作者：{{ book.authorName }}</p>
          <p class="book-desc" @click="bookDetail(book.bookId)">{{ book.bookDesc }}</p>
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.left-box {
  box-sizing: border-box;
  flex: 3;
  padding: 10px;

  .title {
    font-size: 28px;
    font-weight: bold;
    color: #333;
    text-align: left;
    border-bottom: 1px solid #ddd;
    margin-bottom: 30px;
    padding-bottom: 20px;
  }

  .book-content {
    display: flex;
    flex-wrap: wrap; /* 允许换行 */
    gap: 20px;
    justify-content: space-between; /* 子元素间均匀分配空间（可选） */
    .book-item {
      display: flex;
      flex-basis: calc(50% - 10px); /* 每个子元素占据的空间为父容器宽度的50%，减去左右边距各5px以确保间距 */
      box-sizing: border-box; /* 让边框和内填充不影响元素总宽度 */
      .book-img {
        display: block;
        width: 120px;
        height: 150px;
        object-fit: cover;

        &:hover {
          border: 2px solid red;
          color: #F70;
          cursor: pointer;
        }
      }

      .book-info {
        box-sizing: border-box;
        //padding: 10px;
        text-align: left;
        font-size: 12px;
        height: 140px;
        overflow: hidden;

        .book-name {
          display: block;
          font-size: 20px;
          font-weight: bold;
          padding: 0 0 10px 10px;

          &:hover {
            text-decoration: underline;
            color: #F70;
            cursor: pointer;
          }
        }

        .book-author {
          display: block;
          font-size: 14px;
          color: #A5A5A5;
          padding: 0 0 10px 10px;
        }

        .book-desc {
          display: block;
          font-size: 15px;
          padding: 0 0 10px 10px;
          line-height: 22px;
          color: #999999;
          font-weight: bold;

          &:hover {
            text-decoration: underline;
            color: #F70;
            cursor: pointer;
          }
        }

      }
    }
  }
}
</style>